﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class PackageManagementBill : System.Web.UI.Page
{
    MyConnection mcon = null;
    SqlConnection con = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        //if (Session["id"] == null)
        //{
        //    Response.Redirect("Login.aspx");
        //}
        mcon = new MyConnection();
        if (!IsPostBack)
            DisplayManagementBill();
    }

    private void DisplayManagementBill()
    {
        con = mcon.OpenConnection();
        try
        {
            SqlDataAdapter da = new SqlDataAdapter("select * from TopUp", con);
            DataSet ds = new DataSet();
            da.Fill(ds, "TopUp");
            grvTopUp.DataSource = ds.Tables["TopUp"];
            grvTopUp.DataBind();
        }
        catch (Exception ex)
        {
            ex.StackTrace.ToString();
        }
        mcon.CloseConnection(con);
    }

    protected void grvTopUp_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void grvTopUp_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex < 0) return;
        if (e.Row.RowIndex != grvTopUp.EditIndex)
        {
            ((ImageButton)e.Row.Cells[6].FindControl("imgDel")).Attributes.Add("onclick", "return confirm('Are you sure to delete this user')");
        }
    }
    protected void chkAll_CheckedChanged(object sender, EventArgs e)
    {
        try
        {
            CheckBox chkAll = (CheckBox)grvTopUp.HeaderRow.Cells[0].FindControl("chkAll");
            foreach (GridViewRow row in grvTopUp.Rows)
            {
                CheckBox chkDel = (CheckBox)row.Cells[0].FindControl("chkDel");
                chkDel.Checked = chkAll.Checked;
            }
        }
        catch (Exception ex)
        {
            ex.StackTrace.ToString();
        }
    }
    protected void grvTopUp_DataBound(object sender, EventArgs e)
    {
        ((ImageButton)grvTopUp.FooterRow.Cells[0].FindControl("imgDelAll")).Attributes.Add("onclick", "return confirm('Are you sure to delete all user')");
    }
    protected void grvTopUp_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = int.Parse(grvTopUp.DataKeys[e.RowIndex].Value.ToString());
        DeleteManagementBillTopUp(id);
        DisplayManagementBill();
    }

    private void DeleteManagementBillTopUp(int id)
    {
        con = mcon.OpenConnection();
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "DeleteManagementBillTopUp";
            cmd.Parameters.Add(new SqlParameter("@id", id));
            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {

            ex.StackTrace.ToString();
        }
    }

    protected void imgDelAll_Click(object sender, ImageClickEventArgs e)
    {
        foreach (GridViewRow row in grvTopUp.Rows)
        {
            CheckBox chkDel = (CheckBox)row.Cells[0].FindControl("chkDel");
            if (chkDel.Checked == true)
            {
                int id = int.Parse(grvTopUp.DataKeys[row.RowIndex].Value.ToString());
                DeleteManagementBillTopUp(id);
            }
        }
        DisplayManagementBill();
    }

    protected void grvTopUp_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grvTopUp.EditIndex = e.NewEditIndex;
        DisplayManagementBill();
    }

    protected void grvTopUp_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grvTopUp.EditIndex = -1;
        DisplayManagementBill();
    }
    protected void grvTopUp_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int id = int.Parse(grvTopUp.DataKeys[e.RowIndex].Value.ToString());
        string duration = ((TextBox)grvTopUp.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
        string denominations = ((TextBox)grvTopUp.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
        string discount = ((TextBox)grvTopUp.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
        string total = ((TextBox)grvTopUp.Rows[e.RowIndex].Cells[5].Controls[0]).Text;

        string sqlUpdate = "update TopUp set _duration=@1, _denominations=@2, _discount=@3, _total=@4 where id_TU=@5";
        con = mcon.OpenConnection();
        SqlCommand cmd = new SqlCommand(sqlUpdate, con);
        cmd.Parameters.AddWithValue("@1", duration);
        cmd.Parameters.AddWithValue("@2", denominations);
        cmd.Parameters.AddWithValue("@3", discount);
        cmd.Parameters.AddWithValue("@4", total);
        cmd.Parameters.AddWithValue("@5", id);
        cmd.ExecuteNonQuery();
        mcon.CloseConnection(con);
        grvTopUp.EditIndex = -1;
        DisplayManagementBill();
    }
}